package io.gitee.kotle.base.library

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import io.gitee.kotle.android.mvvm.MvvmBottomSheetDialog
import io.gitee.kotle.android.mvvm.MvvmDialog
import io.gitee.kotle.android.mvvm.mvvminterface.MvvmViewType
import io.gitee.kotle.android.utils.StatusData
import io.gitee.kotle.android.utils.StatusDataObserver
import io.gitee.kotle.android.utils.doOnSuccess
import io.gitee.kotle.android.utils.observeStatusData
import io.gitee.kotle.base.OnStatusDataAttachViewListener
import io.gitee.kotle.base.observerStatusDataAttachView

/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2021/11/23 11:14 上午
 *
 */
abstract class BaseBottomSheetDialog : MvvmBottomSheetDialog(), IUiViewHelper by UiViewHelperImpl() {
    override fun beforeMvvmCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): ViewGroup? {
        return createBaseUiView(
            inflater,
            container,
            savedInstanceState,
            isNeedToolBar(),
            isNeedBaseUiView()
        )
    }

    override fun afterMvvmCreateView(
        inflater: LayoutInflater,
        beforeView: ViewGroup?,
        createView: MvvmViewType?,
        savedInstanceState: Bundle?
    ): MvvmViewType? {
        return handleBaseUiView(inflater, beforeView, createView, savedInstanceState)
    }


    /**
     * 绑定data
     */
    inline fun <T> LiveData<StatusData<T>>.observerAttachView(
        crossinline doSuccess: Function1<T, Unit>
    ) =
        observerStatusDataAttachView(
            viewLifecycleOwner,
            this@BaseBottomSheetDialog,
            doSuccess
        )

    fun <T> LiveData<StatusData<T>>.observerAttachView(
        listener: OnStatusDataAttachViewListener<T>,
    ) =
        observerStatusDataAttachView(
            viewLifecycleOwner,
            this@BaseBottomSheetDialog,
            listener
        )

    /**
     * 绑定data
     */
    fun <T> LiveData<StatusData<T>>.observerStatus(block: StatusDataObserver<T>.() -> Unit) =
        observeStatusData(viewLifecycleOwner, block)


    /**
     * 绑定data
     */
    inline fun <T> LiveData<StatusData<T>>.doSuccess(crossinline block: Function1<T, Unit>) =
        doOnSuccess(viewLifecycleOwner, block)
}